/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SellerProcCliente.java
 *
 * Created on 07/12/2009, 22:02:02
 */

package sigbar.view;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Denis
 */
public class SellerProcCliente extends javax.swing.JDialog {

    /** Creates new form SellerProcCliente */
    public SellerProcCliente(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        checkCPF = new javax.swing.JCheckBox();
        textfieldCPF = new javax.swing.JTextField();
        checkNome = new javax.swing.JCheckBox();
        textfieldNome = new javax.swing.JTextField();
        checkNiver = new javax.swing.JCheckBox();
        textfieldNiver = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableCliente = new javax.swing.JTable();
        buttonBuscar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Procurar Clientes");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Busca de Clientes");

        checkCPF.setText("CPF");

        checkNome.setText("Nome");

        checkNiver.setText("Aniversário");

        tableCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "CPF", "Nome", "Aniversário", "E-mail", "Fone"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tableCliente);

        buttonBuscar.setText("Buscar");
        buttonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(checkCPF)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textfieldCPF, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkNome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textfieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkNiver)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textfieldNiver, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(checkCPF)
                    .addComponent(textfieldCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(checkNome)
                    .addComponent(textfieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(checkNiver)
                    .addComponent(textfieldNiver, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBuscarActionPerformed
        ListSelectionModel selecao = tableCliente.getSelectionModel();
        selecao.clearSelection();
        TableModel modelotab = tableCliente.getModel();
        boolean sair = false;
        ArrayList<Integer> selecionados = new ArrayList<Integer>();

        // Procurar pelo CPF
        if ( checkCPF.isSelected() ) {
            if ( !(textfieldCPF.getText().equals("")) ) {
                for (int i = 0; i < modelotab.getRowCount(); i++ ) {
                    if ( textfieldCPF.getText().equals(modelotab.getValueAt(i, 0)) ) {
                        if ( !selecionados.contains(i)) {
                            selecionados.add(i);
                        }
                    } else {
                        if ( selecionados.contains(i) ) {
                            selecionados.remove((Object)i);
                        }
                    }
                }
                if ( selecionados.size() == 0 ) {
                    sair = true;
                }
            } else {
                JOptionPane.showMessageDialog(this, "Escreva o CPF que deseja buscar");
            }
        }

                // Procurar pelo CNPJ
        if ( checkNome.isSelected() ) {
            if ( !(textfieldNome.getText().equals("")) ) {
                for (int i = 0; i < modelotab.getRowCount(); i++ ) {
                    if ( textfieldNome.getText().equals(modelotab.getValueAt(i, 0)) ) {
                        if ( !selecionados.contains(i)) {
                            selecionados.add(i);
                        }
                    } else {
                        if ( selecionados.contains(i) ) {
                            selecionados.remove((Object)i);
                        }
                    }
                }
                if ( selecionados.size() == 0 ) {
                    sair = true;
                }
            } else {
                JOptionPane.showMessageDialog(this, "Escreva o Nome que deseja buscar");
            }
        }

        // Procurar pelo Aniversário
        if ( checkNiver.isSelected() ) {
            if ( !(textfieldNiver.getText().equals("")) ) {
                for (int i = 0; i < modelotab.getRowCount(); i++ ) {
                    if ( textfieldNiver.getText().equals(modelotab.getValueAt(i, 0)) ) {
                        if ( !selecionados.contains(i)) {
                            selecionados.add(i);
                        }
                    } else {
                        if ( selecionados.contains(i) ) {
                            selecionados.remove((Object)i);
                        }
                    }
                }
                if ( selecionados.size() == 0 ) {
                    sair = true;
                }
            } else {
                JOptionPane.showMessageDialog(this, "Escreva o Aniversário que deseja buscar");
            }
        }

        // Selecionar Resultado na tabela
        if ( selecionados.size() == 0 ) {
            JOptionPane.showMessageDialog(this, "Não foi encontrado nenhum registro de Cliente procurado");
        } else {
            for (int i = 0; i < selecionados.size(); i++) {
                selecao.addSelectionInterval(selecionados.get(i), selecionados.get(i));
            }
        }


    }//GEN-LAST:event_buttonBuscarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBuscar;
    private javax.swing.JCheckBox checkCPF;
    private javax.swing.JCheckBox checkNiver;
    private javax.swing.JCheckBox checkNome;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableCliente;
    private javax.swing.JTextField textfieldCPF;
    private javax.swing.JTextField textfieldNiver;
    private javax.swing.JTextField textfieldNome;
    // End of variables declaration//GEN-END:variables

}
